10 class ApiOptionsTest
extends MediaWikiLangTestCase
{
12 /** @var PHPUnit_Framework_MockObject_MockObject */
14 /** @var ApiOptions */
17 /** @var DerivativeContext */
20 private static $Success = [ 'options' => 'success' ];
22 protected function setUp() {
25 $this->mUserMock
= $this->getMockBuilder( User
::class )
26 ->disableOriginalConstructor()
29 // Set up groups and rights
30 $this->mUserMock
->expects( $this->any() )
31 ->method( 'getEffectiveGroups' )->will( $this->returnValue( [ '*', 'user' ] ) );
32 $this->mUserMock
->expects( $this->any() )
33 ->method( 'isAllowedAny' )->will( $this->returnValue( true ) );
35 // Set up callback for User::getOptionKinds
36 $this->mUserMock
->expects( $this->any() )
37 ->method( 'getOptionKinds' )->will( $this->returnCallback( [ $this, 'getOptionKinds' ] ) );
40 $this->mUserMock
->expects( $this->any() )
41 ->method( 'getInstanceForUpdate' )->will( $this->returnValue( $this->mUserMock
) );
43 // Needs to return something
44 $this->mUserMock
->method( 'getOptions' )
47 // Create a new context
48 $this->mContext
= new DerivativeContext( new RequestContext() );
49 $this->mContext
->getContext()->setTitle( Title
::newFromText( 'Test' ) );
50 $this->mContext
->setUser( $this->mUserMock
);
52 $main = new ApiMain( $this->mContext
);
57 $this->mTested
= new ApiOptions( $main, 'options' );
59 $this->mergeMwGlobalArrayValue( 'wgHooks', [
61 [ $this, 'hookGetPreferences' ]
66 public function hookGetPreferences( $user, &$preferences ) {
69 foreach ( [ 'name', 'willBeNull', 'willBeEmpty', 'willBeHappy' ] as $k ) {
73 'label' => "\u{00A0}",
77 $preferences['testmultiselect'] = [
78 'type' => 'multiselect',
81 '<span dir="auto">Some HTML here for option 1</span>' => 'opt1',
82 '<span dir="auto">Some HTML here for option 2</span>' => 'opt2',
83 '<span dir="auto">Some HTML here for option 3</span>' => 'opt3',
84 '<span dir="auto">Some HTML here for option 4</span>' => 'opt4',
88 'label' => "\u{00A0}",
89 'prefix' => 'testmultiselect-',
97 * @param IContextSource $context
98 * @param array|null $options
102 public function getOptionKinds( IContextSource
$context, $options = null ) {
105 'name' => 'registered',
106 'willBeNull' => 'registered',
107 'willBeEmpty' => 'registered',
108 'willBeHappy' => 'registered',
109 'testmultiselect-opt1' => 'registered-multiselect',
110 'testmultiselect-opt2' => 'registered-multiselect',
111 'testmultiselect-opt3' => 'registered-multiselect',
112 'testmultiselect-opt4' => 'registered-multiselect',
113 'special' => 'special',
116 if ( $options === null ) {
121 foreach ( $options as $key => $value ) {
122 if ( isset( $kinds[$key] ) ) {
123 $mapping[$key] = $kinds[$key];
124 } elseif ( substr( $key, 0, 7 ) === 'userjs-' ) {
125 $mapping[$key] = 'userjs';
127 $mapping[$key] = 'unused';
134 private function getSampleRequest( $custom = [] ) {
138 'optionname' => null,
139 'optionvalue' => null,
142 return array_merge( $request, $custom );
145 private function executeQuery( $request ) {
146 $this->mContext
->setRequest( new FauxRequest( $request, true, $this->mSession
) );
147 $this->mTested
->execute();
149 return $this->mTested
->getResult()->getResultData( null, [ 'Strip' => 'all' ] );
153 * @expectedException ApiUsageException
155 public function testNoToken() {
156 $request = $this->getSampleRequest( [ 'token' => null ] );
158 $this->executeQuery( $request );
161 public function testAnon() {
162 $this->mUserMock
->expects( $this->once() )
164 ->will( $this->returnValue( true ) );
167 $request = $this->getSampleRequest();
169 $this->executeQuery( $request );
170 } catch ( ApiUsageException
$e ) {
171 $this->assertTrue( ApiTestCase
::apiExceptionHasCode( $e, 'notloggedin' ) );
174 $this->fail( "ApiUsageException was not thrown" );
177 public function testNoOptionname() {
179 $request = $this->getSampleRequest( [ 'optionvalue' => '1' ] );
181 $this->executeQuery( $request );
182 } catch ( ApiUsageException
$e ) {
183 $this->assertTrue( ApiTestCase
::apiExceptionHasCode( $e, 'nooptionname' ) );
186 $this->fail( "ApiUsageException was not thrown" );
189 public function testNoChanges() {
190 $this->mUserMock
->expects( $this->never() )
191 ->method( 'resetOptions' );
193 $this->mUserMock
->expects( $this->never() )
194 ->method( 'setOption' );
196 $this->mUserMock
->expects( $this->never() )
197 ->method( 'saveSettings' );
200 $request = $this->getSampleRequest();
202 $this->executeQuery( $request );
203 } catch ( ApiUsageException
$e ) {
204 $this->assertTrue( ApiTestCase
::apiExceptionHasCode( $e, 'nochanges' ) );
207 $this->fail( "ApiUsageException was not thrown" );
210 public function testReset() {
211 $this->mUserMock
->expects( $this->once() )
212 ->method( 'resetOptions' )
213 ->with( $this->equalTo( [ 'all' ] ) );
215 $this->mUserMock
->expects( $this->never() )
216 ->method( 'setOption' );
218 $this->mUserMock
->expects( $this->once() )
219 ->method( 'saveSettings' );
221 $request = $this->getSampleRequest( [ 'reset' => '' ] );
223 $response = $this->executeQuery( $request );
225 $this->assertEquals( self
::$Success, $response );
228 public function testResetKinds() {
229 $this->mUserMock
->expects( $this->once() )
230 ->method( 'resetOptions' )
231 ->with( $this->equalTo( [ 'registered' ] ) );
233 $this->mUserMock
->expects( $this->never() )
234 ->method( 'setOption' );
236 $this->mUserMock
->expects( $this->once() )
237 ->method( 'saveSettings' );
239 $request = $this->getSampleRequest( [ 'reset' => '', 'resetkinds' => 'registered' ] );
241 $response = $this->executeQuery( $request );
243 $this->assertEquals( self
::$Success, $response );
246 public function testOptionWithValue() {
247 $this->mUserMock
->expects( $this->never() )
248 ->method( 'resetOptions' );
250 $this->mUserMock
->expects( $this->once() )
251 ->method( 'setOption' )
252 ->with( $this->equalTo( 'name' ), $this->equalTo( 'value' ) );
254 $this->mUserMock
->expects( $this->once() )
255 ->method( 'saveSettings' );
257 $request = $this->getSampleRequest( [ 'optionname' => 'name', 'optionvalue' => 'value' ] );
259 $response = $this->executeQuery( $request );
261 $this->assertEquals( self
::$Success, $response );
264 public function testOptionResetValue() {
265 $this->mUserMock
->expects( $this->never() )
266 ->method( 'resetOptions' );
268 $this->mUserMock
->expects( $this->once() )
269 ->method( 'setOption' )
270 ->with( $this->equalTo( 'name' ), $this->identicalTo( null ) );
272 $this->mUserMock
->expects( $this->once() )
273 ->method( 'saveSettings' );
275 $request = $this->getSampleRequest( [ 'optionname' => 'name' ] );
276 $response = $this->executeQuery( $request );
278 $this->assertEquals( self
::$Success, $response );
281 public function testChange() {
282 $this->mUserMock
->expects( $this->never() )
283 ->method( 'resetOptions' );
285 $this->mUserMock
->expects( $this->exactly( 3 ) )
286 ->method( 'setOption' )
288 [ $this->equalTo( 'willBeNull' ), $this->identicalTo( null ) ],
289 [ $this->equalTo( 'willBeEmpty' ), $this->equalTo( '' ) ],
290 [ $this->equalTo( 'willBeHappy' ), $this->equalTo( 'Happy' ) ]
293 $this->mUserMock
->expects( $this->once() )
294 ->method( 'saveSettings' );
296 $request = $this->getSampleRequest( [
297 'change' => 'willBeNull|willBeEmpty=|willBeHappy=Happy'
300 $response = $this->executeQuery( $request );
302 $this->assertEquals( self
::$Success, $response );
305 public function testResetChangeOption() {
306 $this->mUserMock
->expects( $this->once() )
307 ->method( 'resetOptions' );
309 $this->mUserMock
->expects( $this->exactly( 2 ) )
310 ->method( 'setOption' )
312 [ $this->equalTo( 'willBeHappy' ), $this->equalTo( 'Happy' ) ],
313 [ $this->equalTo( 'name' ), $this->equalTo( 'value' ) ]
316 $this->mUserMock
->expects( $this->once() )
317 ->method( 'saveSettings' );
321 'change' => 'willBeHappy=Happy',
322 'optionname' => 'name',
323 'optionvalue' => 'value'
326 $response = $this->executeQuery( $this->getSampleRequest( $args ) );
328 $this->assertEquals( self
::$Success, $response );
331 public function testMultiSelect() {
332 $this->mUserMock
->expects( $this->never() )
333 ->method( 'resetOptions' );
335 $this->mUserMock
->expects( $this->exactly( 4 ) )
336 ->method( 'setOption' )
338 [ $this->equalTo( 'testmultiselect-opt1' ), $this->identicalTo( true ) ],
339 [ $this->equalTo( 'testmultiselect-opt2' ), $this->identicalTo( null ) ],
340 [ $this->equalTo( 'testmultiselect-opt3' ), $this->identicalTo( false ) ],
341 [ $this->equalTo( 'testmultiselect-opt4' ), $this->identicalTo( false ) ]
344 $this->mUserMock
->expects( $this->once() )
345 ->method( 'saveSettings' );
347 $request = $this->getSampleRequest( [
348 'change' => 'testmultiselect-opt1=1|testmultiselect-opt2|'
349 . 'testmultiselect-opt3=|testmultiselect-opt4=0'
352 $response = $this->executeQuery( $request );
354 $this->assertEquals( self
::$Success, $response );
357 public function testSpecialOption() {
358 $this->mUserMock
->expects( $this->never() )
359 ->method( 'resetOptions' );
361 $this->mUserMock
->expects( $this->never() )
362 ->method( 'saveSettings' );
364 $request = $this->getSampleRequest( [
365 'change' => 'special=1'
368 $response = $this->executeQuery( $request );
370 $this->assertEquals( [
371 'options' => 'success',
374 'warnings' => "Validation error for \"special\": cannot be set by this module."
380 public function testUnknownOption() {
381 $this->mUserMock
->expects( $this->never() )
382 ->method( 'resetOptions' );
384 $this->mUserMock
->expects( $this->never() )
385 ->method( 'saveSettings' );
387 $request = $this->getSampleRequest( [
388 'change' => 'unknownOption=1'
391 $response = $this->executeQuery( $request );
393 $this->assertEquals( [
394 'options' => 'success',
397 'warnings' => "Validation error for \"unknownOption\": not a valid preference."
403 public function testUserjsOption() {
404 $this->mUserMock
->expects( $this->never() )
405 ->method( 'resetOptions' );
407 $this->mUserMock
->expects( $this->once() )
408 ->method( 'setOption' )
409 ->with( $this->equalTo( 'userjs-option' ), $this->equalTo( '1' ) );
411 $this->mUserMock
->expects( $this->once() )
412 ->method( 'saveSettings' );
414 $request = $this->getSampleRequest( [
415 'change' => 'userjs-option=1'
418 $response = $this->executeQuery( $request );
420 $this->assertEquals( self
::$Success, $response );